package jbit.dao.impl;

import java.io.Serializable;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.sun.org.apache.bcel.internal.generic.NEW;

import jbit.dao.ClaimVoucherDetailDao;
import jbit.entity.ClaimVoucherDetail;

public class ClaimVoucherDetailDaoHibImpl extends HibernateDaoSupport
		implements ClaimVoucherDetailDao {
		
	  public ClaimVoucherDetail add(ClaimVoucherDetail claimVoucherDetail){
		  super.getHibernateTemplate().save(claimVoucherDetail);
		  return claimVoucherDetail;
	  }
	  public void deleteById(Serializable id){
		  super.getHibernateTemplate().delete(this.get(id));
	  }
	  public void update(ClaimVoucherDetail claimVoucherDetail){
		  super.getHibernateTemplate().update(claimVoucherDetail);
	  }
	  public ClaimVoucherDetail get(Serializable id){
		  ClaimVoucherDetail claimVoucherDetail=(ClaimVoucherDetail)super.getHibernateTemplate().get(ClaimVoucherDetail.class, id);
		  return claimVoucherDetail;
	  }
	  public List<ClaimVoucherDetail> find(ClaimVoucherDetail condition){
		  List<ClaimVoucherDetail> claimVoucherDetaiList=super.getHibernateTemplate().findByExample(condition);
		  return claimVoucherDetaiList;
	  }
	 
	  public double getTotalAcount(Serializable id){
//
//		    double total=0;
//			if(id.equals(0)){
//				return total;
//			}else{
//				List<ClaimVoucherDetail> list =super.getHibernateTemplate()
//				.find("from ClaimVoucherDetail cvd where cvd.bizClaimVoucher.id=" + id);
//				for(ClaimVoucherDetail claimVoucherDetail: list){
//					total += claimVoucherDetail.getAccount();
//				}
//				return total;
//			}
//			
//			  System.out.println("id "+id);
//			  String sql ="SELECT  sum(c.account) as totalAccount from ClaimVoucherDetail AS c  where c.bizClaimVoucher.id=?";
//			  List list=super.getHibernateTemplate().find(sql,new Object[] {id});
//			  System.out.println("list ="+list);
//		      return (Double)list.get(0) == null ? 0 :(Double)list.get(0);  
		      
          System.out.println("id "+id);
		  String sql ="SELECT  ISNULL(sum(c.account),0) AS totalAccount  FROM  ClaimVoucherDetail AS c  where c.bizClaimVoucher.id=?";
		  List list=super.getHibernateTemplate().find(sql,new Object[]{id});
		  System.out.println("list ="+list);
	      return (Double)list.get(0) ;  

		 

		  

			
	  }
	  //根据主单返回明细
	  public List<ClaimVoucherDetail> getDetails(Serializable id){
		  return  super.getHibernateTemplate().find("from ClaimVoucherDetail c where c.bizClaimVoucher.id=?",new Object[]{id});
	  }
}
